//
//  EFengOrganizationCell.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/2/16.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengOrganizationCell.h"
#import "WZTabBarButton.h"
#import "CContactModel.h"
#import "CDepartmentModel.h"
#import "UIImageView+WebCache.h"
#import "CXmppRequestMgr.h"


@interface EFengOrganizationCell ()

WeakProperty UIImageView *iconImageView;
WeakProperty UILabel *nameLabel;
WeakProperty UILabel *iconLabel;


@end

@implementation EFengOrganizationCell

+ (instancetype)organizationCellWithCollectionview:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath
{
    static NSString *organizationId = @"organizationCell";
    EFengOrganizationCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:organizationId forIndexPath:indexPath];
    cell.cellIndexPath = indexPath;
    return cell;
}

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        UIImageView *img = [[UIImageView alloc] init];
        [self.contentView addSubview:img];
        self.iconImageView = img;
        UILabel *iconLabel = [[UILabel alloc]init];
        iconLabel.textColor = [UIColor whiteColor];
        iconLabel.textAlignment = NSTextAlignmentCenter;
        iconLabel.font = Font15;
        iconLabel.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
        [self.iconImageView addSubview:iconLabel];
        self.iconLabel = iconLabel;
        UILabel *label = [[UILabel alloc] init];
        label.textColor = RGBCOLORWITH16COLOR(0x323232);
        label.textAlignment = NSTextAlignmentCenter;
        label.font = Font12;
        [self.contentView addSubview:label];
        self.nameLabel = label;
        
    }
    return self;
}

- (void)setOrganization:(id)organization
{
    _organization = organization;
    self.iconImageView.image = nil;
    self.iconLabel.backgroundColor = [UIColor clearColor];
    self.iconLabel.text = nil;
    if ([organization isKindOfClass:[CContactModel class]]) {
        CContactModel *contactModel = (CContactModel *)organization;
         [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:contactModel.UserID]] placeholderImage:[UIImage imageNamed:@"80-80"]];
        if ([contactModel.IsAvailable isEqualToString:@"在线"]) {
            
            self.iconImageView.alpha = 1.0f;
            
        } else {
            
            self.iconImageView.alpha = 0.4f;

        }
        self.nameLabel.text = contactModel.UserName;
    } else if ([organization isKindOfClass:[CDepartmentModel class]]) {
        self.iconImageView.backgroundColor = self.color;
        CDepartmentModel *dep = (CDepartmentModel *)organization;
        NSString *str =[dep.DepartName substringToIndex:1];
        unichar c = [dep.DepartName characterAtIndex:0];
        if (c >=0x4E00 && c <=0x9FFF)
        {
            self.iconLabel.font = Font15;
        }
        else
        {
            self.iconLabel.font = [UIFont systemFontOfSize:20];
        }
        self.iconLabel.text = str;
        self.iconImageView.alpha = 1.0f;

    
        [self.nameLabel setText:dep.DepartName];

    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
//    self.button.frame = self.bounds;
    self.iconImageView.frame = CGRectMake((self.width - 40.0f * HARDWARE_SCREEN_WIDTH / 320.0f) / 2.0f, 5.0f, 40.0f * HARDWARE_SCREEN_WIDTH / 320.0f, 40.0f * HARDWARE_SCREEN_WIDTH / 320.0f);
    self.iconLabel.size = CGSizeMake(self.iconImageView.size.width, self.iconImageView.size.height);
    self.iconLabel.center = CGPointMake(self.iconImageView.size.width/2, self.iconImageView.size.height/2);
    self.nameLabel.frame = CGRectMake(0, CGRectGetMaxY(self.iconImageView.frame), self.width, self.height - CGRectGetMaxY(self.iconImageView.frame));
    self.iconImageView.layer.cornerRadius = self.iconImageView.frame.size.width/2;
    self.iconImageView.layer.masksToBounds = YES;
}

@end
